package labox.innovation.gameserver;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javolution.util.FastList;
import javolution.util.FastMap;
import labox.innovation.config.Config;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.L2GameServerPacket;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;

/**
 * This class stores references to all online game masters. (access level > 100)
 * 
 * @version $Revision: 1.2.2.1.2.7 $ $Date: 2005/04/05 19:41:24 $
 */
public class GmListTable {
	private static Logger _log = LoggerFactory.getLogger(GmListTable.class.getName());

	/** Set(L2PcInstance>) containing all the GM in game */
	private FastMap<FuzePcInstance, Boolean> _gmList;

	public static GmListTable getInstance() {
		return SingletonHolder._instance;
	}

	public FastList<FuzePcInstance> getAllGms(boolean includeHidden) {
		FastList<FuzePcInstance> tmpGmList = new FastList<FuzePcInstance>();

		for (FastMap.Entry<FuzePcInstance, Boolean> n = _gmList.head(), end = _gmList.tail(); (n = n.getNext()) != end;)
			if (includeHidden || !n.getValue())
				tmpGmList.add(n.getKey());

		return tmpGmList;
	}

	public FastList<String> getAllGmNames(boolean includeHidden) {
		FastList<String> tmpGmList = new FastList<String>();

		for (FastMap.Entry<FuzePcInstance, Boolean> n = _gmList.head(), end = _gmList.tail(); (n = n.getNext()) != end;)
			if (!n.getValue())
				tmpGmList.add(n.getKey().getName());
			else if (includeHidden)
				tmpGmList.add(n.getKey().getName() + " (invis)");

		return tmpGmList;
	}

	private GmListTable() {
		_gmList = new FastMap<FuzePcInstance, Boolean>().shared();
	}

	/**
	 * Add a L2PcInstance player to the Set _gmList
	 */
	public void addGm(FuzePcInstance player, boolean hidden) {
		if (_log.isDebugEnabled())
			_log.debug("added gm: " + player.getName());
		_gmList.put(player, hidden);
	}

	public void deleteGm(FuzePcInstance player) {
		if (_log.isDebugEnabled())
			_log.debug("deleted gm: " + player.getName());

		_gmList.remove(player);
	}

	/**
	 * GM will be displayed on clients gmlist
	 * @param player
	 */
	public void showGm(FuzePcInstance player) {
		FastMap.Entry<FuzePcInstance, Boolean> gm = _gmList.getEntry(player);
		if (gm != null)
			gm.setValue(false);
	}

	/**
	 * GM will no longer be displayed on clients gmlist
	 * @param player
	 */
	public void hideGm(FuzePcInstance player) {
		FastMap.Entry<FuzePcInstance, Boolean> gm = _gmList.getEntry(player);
		if (gm != null)
			gm.setValue(true);
	}

	public boolean isGmOnline(boolean includeHidden) {
		for (FastMap.Entry<FuzePcInstance, Boolean> n = _gmList.head(), end = _gmList.tail(); (n = n.getNext()) != end;) {
			if (includeHidden || !n.getValue())
				return true;
		}

		return false;
	}

	public void sendListToPlayer(FuzePcInstance player) {
		if (!isGmOnline(player.isGM())) {
			SystemMessage sm = new SystemMessage(SystemMessageId.NO_GM_PROVIDING_SERVICE_NOW); //There are not any GMs that are providing customer service currently.
			player.sendPacket(sm);
		} else {
			SystemMessage sm = new SystemMessage(SystemMessageId.GM_LIST);
			player.sendPacket(sm);

			for (String name : getAllGmNames(player.isGM())) {
				sm = new SystemMessage(SystemMessageId.GM_C1);
				sm.addString(name);
				player.sendPacket(sm);
			}
		}
	}

	public static void broadcastToGMs(L2GameServerPacket packet) {
		for (FuzePcInstance gm : getInstance().getAllGms(true)) {
			gm.sendPacket(packet);
		}
	}

	public static void broadcastMessageToGMs(String message) {
		for (FuzePcInstance gm : getInstance().getAllGms(true)) {
			gm.sendMessage(message);
		}
	}

	@SuppressWarnings("synthetic-access")
	private static class SingletonHolder {
		protected static final GmListTable _instance = new GmListTable();
	}
}
